package com.edu.hre.db.sql.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreSkill;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.admin.SkillForm;
import com.edu.hre.struts.form.admin.SkillListForm;

public class SkillDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "skillDAOService";
	private BaseService baseService  = null;
	 
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	} 

	public ArrayList<SkillForm> getSkillListInfo(int limit, int offset){

		List<HreSkill> hreSkills = baseService.getPojos(null, -1, null, new String[]{"dateCreated"}, new Boolean[]{true}, limit, offset, HreSkill.class);
		ArrayList<SkillForm> skills = new ArrayList<SkillForm>();
		
		if (hreSkills == null) {
			return skills;
		}
		for (HreSkill hreEdu: hreSkills) {

			try {

				SkillForm skill = new SkillForm();
				skill.setId(hreEdu.getId() + "");
				skill.setName(CommonUtils.normalizeString(hreEdu.getName()));
				skill.setDescription(CommonUtils.normalizeString(hreEdu.getDescription()));
				
				skills.add(skill);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		
		return skills;
	}
	
	public int countSkills(){

		int count = baseService.countByColumns(null, null, null, "id", HreSkill.class);
		return count < 0 ? 0 : count;
	}

	public void deleteSkills(SkillListForm form){

		ArrayList<SkillForm> info = (ArrayList<SkillForm>) form.getAllSkills();

		for (int i = 0; i < info.size(); i++) {

			try {

				SkillForm skill = info.get(i);
				if (skill.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, Integer.parseInt(skill.getId()), null, null, 0, HreSkill.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}